package com.kingdee.pda.ui.set_config;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.example.basecommon.utils.PreferencesUtils;
import com.example.basecommon.utils.SpeakUtils;
import com.kingdee.pda.R;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.ui.activity.BaseTitleActivity;
import com.kingdee.pda.ui.widget.CleanableEditText;
import com.kingdee.pda.util.CommonUtil;
import com.kingdee.pda.util.ConstantPreference;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 条码截取配置
 * * Created by dlj
 * DATE 2022/4/17
 * TIME 15:20
 */
public class OutWarehouseConDetailActivity extends BaseTitleActivity {

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;

    @BindView(R.id.et_limit_start)
    CleanableEditText et_limit_start;
    @BindView(R.id.et_limit_end)
    CleanableEditText et_limit_end;
    @BindView(R.id.et_before_num)
    CleanableEditText et_before_num;
    @BindView(R.id.et_after_num)
    CleanableEditText et_after_num;
    @BindView(R.id.et_delete_before_num)
    CleanableEditText et_delete_before_num;
    @BindView(R.id.et_delete_after_num)
    CleanableEditText et_delete_after_num;
    private String type;

    @Override
    public void changeWarehouse(Warehouse warehouse) {

    }

    @Override
    protected void onActivityCreate(Bundle savedInstanceState) {
        super.onActivityCreate(savedInstanceState);
        setContentView(R.layout.activity_outwarehoust_con_detail);
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("取数规则配置");

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                switch (checkId) {
                    case R.id.rb_limit:
                        clearAllEdit();
                        CommonUtil.requestFocusAndNoClearText(et_limit_start);
                        break;
                    case R.id.rb_before_x:
                        clearAllEdit();
                        CommonUtil.requestFocusAndNoClearText(et_before_num);
                        break;
                    case R.id.rb_after_x:
                        clearAllEdit();
                        CommonUtil.requestFocusAndNoClearText(et_after_num);
                        break;
                    case R.id.rb_delete_before_x:
                        clearAllEdit();
                        CommonUtil.requestFocusAndNoClearText(et_delete_before_num);
                        break;
                    case R.id.rb_delete_after_x:
                        clearAllEdit();
                        CommonUtil.requestFocusAndNoClearText(et_delete_after_num);
                        break;
                    default:
                        break;
                }
            }
        });

        type = getIntent().getStringExtra("type");
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @OnClick({R.id.btn_fiinish, R.id.btn_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_fiinish:
                if (checkInput()) {
                    finish();
                } else {
                    SpeakUtils.speakWithToast(this, "请检查截取规则的输入是否完善", false, false);
                }
                break;
            case R.id.btn_cancel:
                finish();
                break;
        }
    }

    /**
     * 检验输入是否规范，如输入无误则保存到sp
     *
     * @return
     */
    private boolean checkInput() {
        boolean isFinishInput = false;
        switch (radioGroup.getCheckedRadioButtonId()) {
            case R.id.rb_limit:
                String startNum = et_limit_start.getText().toString();
                String endNum = et_limit_end.getText().toString();

                if (!TextUtils.isEmpty(startNum) && !TextUtils.isEmpty(endNum)) {
                    if (Integer.parseInt(startNum) > Integer.parseInt(endNum)) {
                        SpeakUtils.speakWithToast(this, "开始下标不能大于结束下标", true, false);
                    } else {
                        isFinishInput = true;
                        //  判断是配置的sku还是sn
                        if ("sku".equals(type)) {
                            PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_RADIOBUTTONID, 0);
                            PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_BEFOREX, Integer.parseInt(startNum));
                            PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_AFTERX, Integer.parseInt(endNum));
                        } else {
                            PreferencesUtils.putInt(this, ConstantPreference.QR_SN_RADIOBUTTONID, 0);
                            PreferencesUtils.putInt(this, ConstantPreference.QR_SN_BEFOREX, Integer.parseInt(startNum));
                            PreferencesUtils.putInt(this, ConstantPreference.QR_SN_AFTERX, Integer.parseInt(endNum));
                        }
                    }
                }
                break;
            case R.id.rb_before_x:
                String beforeNum = et_before_num.getText().toString();
                if (!TextUtils.isEmpty(beforeNum)) {
                    isFinishInput = true;
                    //  判断是配置的sku还是sn
                    if ("sku".equals(type)) {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_RADIOBUTTONID, 1);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_BEFOREX, Integer.parseInt(beforeNum));
                    } else {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_RADIOBUTTONID, 1);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_BEFOREX, Integer.parseInt(beforeNum));
                    }
                }
                break;
            case R.id.rb_after_x:
                String afterNum = et_after_num.getText().toString();
                if (!TextUtils.isEmpty(afterNum)) {
                    isFinishInput = true;
                    //  判断是配置的sku还是sn
                    if ("sku".equals(type)) {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_RADIOBUTTONID, 2);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_AFTERX, Integer.parseInt(afterNum));
                    } else {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_RADIOBUTTONID, 2);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_AFTERX, Integer.parseInt(afterNum));
                    }
                }
                break;
            case R.id.rb_delete_before_x:
                String deleteBeforeNum = et_delete_before_num.getText().toString();
                if (!TextUtils.isEmpty(deleteBeforeNum)) {
                    isFinishInput = true;
                    //  判断是配置的sku还是sn
                    if ("sku".equals(type)) {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_RADIOBUTTONID, 3);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_BEFOREX, Integer.parseInt(deleteBeforeNum));
                    } else {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_RADIOBUTTONID, 3);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_BEFOREX, Integer.parseInt(deleteBeforeNum));
                    }
                }
                break;
            case R.id.rb_delete_after_x:
                String deleteAfterNum = et_delete_after_num.getText().toString();
                if (!TextUtils.isEmpty(deleteAfterNum)) {
                    isFinishInput = true;
                    //  判断是配置的sku还是sn
                    if ("sku".equals(type)) {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_RADIOBUTTONID, 4);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SKU_AFTERX, Integer.parseInt(deleteAfterNum));
                    } else {
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_RADIOBUTTONID, 4);
                        PreferencesUtils.putInt(this, ConstantPreference.QR_SN_AFTERX, Integer.parseInt(deleteAfterNum));
                    }
                }
                break;
        }
        return isFinishInput;
    }

    private void clearAllEdit() {
        et_limit_start.setText("");
        et_limit_end.setText("");
        et_before_num.setText("");
        et_after_num.setText("");
        et_delete_before_num.setText("");
        et_delete_after_num.setText("");
    }
}
